package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.PermissionMapper;
import com.zhongyu.mapper.RoleMapper;
import com.zhongyu.mapper.RolePermissionMapper;
import com.zhongyu.mapper.UserMapper;
import com.zhongyu.pojo.Permission;
import com.zhongyu.pojo.Role;
import com.zhongyu.pojo.RolePermission;
import com.zhongyu.pojo.User;
import com.zhongyu.service.PermissionService;
import com.zhongyu.vo.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("permissionService")
@Slf4j
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper,Permission> implements PermissionService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private RoleMapper roleMapper;

    private List<TreeNode> searchByParentId(Integer parentId,Integer roleId){
       List<Integer> permIds=rolePermissionMapper.findPermIds(roleId);
        List<TreeNode> nodes=new ArrayList<>();
        List<Permission> list = getPermissions(parentId);

        for (Permission p:list){
       TreeNode node=new TreeNode();
         node.setId(p.getId());
         node.setExpand(false);
         QueryWrapper queryWrapper=new QueryWrapper();
         queryWrapper.eq("parent_id",p.getId());
       Integer count=  this.baseMapper.selectCount(queryWrapper);
         if (permIds.contains(p.getId())&&count==0){
             node.setChecked(true);
         }
         node.setTitle(p.getName());
       nodes.add(node);
     }
     return nodes;
    }

    private List<Permission> getPermissions(Integer parentId) {
        QueryWrapper wrapper= Wrappers.<Permission>query();
        if (parentId==null) {
            wrapper.isNull("parent_id");
        }else {

            wrapper.eq("parent_id",parentId);
        }
        wrapper.orderByAsc("sort");
        return super.list(wrapper);
    }


    @Override
    public List<TreeNode> search4Tree(Integer id) {
    List<TreeNode> nodes01=this.searchByParentId(null,id);

    for (TreeNode node01:nodes01){
        List<TreeNode> nodes02=this.searchByParentId(node01.getId(),id);
        node01.setChildren(nodes02);

        for (TreeNode node02:nodes02){
         List<TreeNode> nodes03= this.searchByParentId(node02.getId(),id);
          node02.setChildren(nodes03);
        }
    }
        return nodes01;
    }


    @Override
    public void grant(Integer roleId, Integer[] ids) {
     rolePermissionMapper.deleteById(roleId);
     if (ids!=null&&ids.length>0){
         for (Integer id:ids){
             RolePermission rolePermission=new RolePermission(roleId,id);
             rolePermissionMapper.insert(rolePermission);
         }
     }
    }
    @Override
    public List<Role> searchAll(Integer id) {
     //   User user=userMapper.selectById(id);
        // Integer roleId=user.getUserRole();
  return roleMapper.selectList(null);
    }
int count=0;
    @Override
    public List<String> searchPermCodes(Integer roleId) {
        log.debug("*************统计searchPermCodes方法执行的次数****************"+(++count));
        return baseMapper.findPermCodes(roleId);
    }

    @Override
    public List<Permission> searchMenuList() {
        Subject subject=SecurityUtils.getSubject();
        List<Permission> menuList01=this.getPermissions(null);
        menuList01= menuList01.stream().filter(p->subject.isPermitted(p.getPermCode()))
                     .collect(Collectors.toList());
        for (Permission p01:menuList01){
List<Permission> menuList02=this.getPermissions(p01.getId());
p01.getChildren().addAll(menuList02);
        }
        return menuList01;
    }


}
